﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.OMS.C;
using Me.Amon.Util;

namespace Me.Amon.POS
{
    public partial class UpdateLevel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < IUser.LEVEL_01)
            {
                Response.Redirect(IEnv.HOME_PAGE);
                return;
            }

            var dba = new RDBEngine();
            ListLevel(dba);

            var lev = Request["lev"];
            if (CharUtil.IsValidateHash(lev))
            {
                LbLevel.SelectedValue = lev;

                dba.Reset();
                ReadRule(dba, lev);
            }
            else
            {
                LbLevel.SelectedIndex = 0;
                ShowDef();
            }
        }

        #region 事件处理
        protected void BtUpdate_Click(object sender, EventArgs e)
        {
            SaveRule();
        }

        protected void BtCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/POS/Vips.aspx?lev=" + LbLevel.SelectedValue);
        }
        #endregion

        private void ListLevel(RDBEngine dba)
        {
            dba.AddTable(DCustomerLevel.TABLE);
            dba.AddColumn(DCustomerLevel.ID);
            dba.AddColumn(DCustomerLevel.NAME);
            dba.AddSort(DCustomerLevel.POINT, true);

            LbLevel.DataSource = dba.ExecuteSelect();
            LbLevel.DataValueField = DCustomerLevel.ID;
            LbLevel.DataTextField = DCustomerLevel.NAME;
            LbLevel.DataBind();
        }

        private void ReadRule(RDBEngine dba, string lev)
        {
            dba.AddTable(DCustomerLevel.TABLE);
            dba.AddWhere(DCustomerLevel.ID, lev);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HfId.Value = lev;
            TbLevel.Text = "" + row[DCustomerLevel.NAME];
            TbPoint.Text = "" + row[DCustomerLevel.POINT];
            TbM2p.Text = "" + row[DCustomerLevel.M2P];
            TbP2m.Text = "" + row[DCustomerLevel.P2M];
            CkStatus.Checked = (CCustomerLevel.STATUS_DISABLED == (int)row[DCustomerLevel.STATUS]);
        }

        private void SaveRule()
        {
            LlErr.Text = "";
            var id = HfId.Value;

            var level = TbLevel.Text.Trim();
            if (!WebUtil.IsValid(level, "会员等级", CCustomerLevel.NAME, LlErr))
            {
                return;
            }

            #region 名称重复性验证
            var dba = new RDBEngine();
            dba.AddTable(DCustomerLevel.TABLE);
            dba.AddColumn(DCustomerLevel.NAME);
            dba.AddWhere(DCustomerLevel.NAME, level);
            dba.AddWhere(DCustomerLevel.ID, "!=", id, true);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 0)
            {
                LlErr.Text = string.Format("会员级别 {0} 已存在！", level);
                return;
            }
            #endregion

            var point = TbPoint.Text.Trim();
            if (!WebUtil.IsValid(point, "所需积分", LlErr))
            {
                return;
            }

            var m2p = TbM2p.Text.Trim();
            if (string.IsNullOrWhiteSpace(m2p) || !Regex.IsMatch(m2p, "^\\d+(.\\d{1,2})?$"))
            {
                LlErr.Text = "请输入有效的积分规则！";
                return;
            }

            var p2m = TbP2m.Text.Trim();
            if (string.IsNullOrWhiteSpace(p2m) || !Regex.IsMatch(p2m, "^\\d+$"))
            {
                LlErr.Text = "请输入有效的兑换规则！";
                return;
            }

            var user = WebUtil.CurrentUser(Session);

            dba.Reset();
            dba.AddTable(DCustomerLevel.TABLE);
            dba.AddParam(DCustomerLevel.NAME, level);
            dba.AddParam(DCustomerLevel.POINT, point);
            dba.AddParam(DCustomerLevel.M2P, m2p);
            dba.AddParam(DCustomerLevel.P2M, p2m);
            dba.AddParam(DCustomerLevel.STATUS, CkStatus.Checked ? CCustomerLevel.STATUS_DISABLED : CCustomerLevel.STATUE_ENABLED);
            dba.AddParam(DCustomerLevel.UPDATE_TIME, DCustomerLevel.NOW, false);
            dba.AddParam(DCustomerLevel.UPDATE_USER, user.UserId);

            if (CharUtil.IsValidateHash(id))
            {
                dba.AddWhere(DCustomerLevel.ID, id);
                dba.ExecuteUpdate();

                Response.Redirect("~/POS/Vips.aspx?lev=" + LbLevel.SelectedValue);
            }
            else
            {
                dba.AddWhere(DCustomerLevel.ID, HashUtil.UtcTimeInHex());
                dba.AddParam(DCustomerLevel.CREATE_TIME, DCustomerLevel.NOW, false);
                dba.AddParam(DCustomerLevel.CREATE_USER, user.UserId);
                dba.ExecuteInsert();

                ShowDef();
            }
        }

        private void ShowDef()
        {
            HfId.Value = "";
            TbLevel.Text = "";
            TbPoint.Text = "0";
            TbM2p.Text = "0";
            TbP2m.Text = "0";
        }
    }
}